<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/1/13 00:50
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Address extends WX_Controller
{
    /**
     * 初始化
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function addrLists()
    {
        if (!empty($this->openId)) {

            $this->load->model('wx/user/Social_Model', 'social');
            $user = $this->social->getUserByOpenId($this->openId);
            if (empty($user)) {
                $this->outputError('用户不存在');
            }

            $this->load->model('wx/user/Address_Model', 'addr');
            $lists = $this->addr->get($user['userid']);
            $this->output($lists);

        } else {
            $this->outputError('用户未登录');
        }
    }

    /**
     * 添加
     */
    public function add()
    {
        if (empty($this->openId)) {
            $this->outputError('用户未登录');
        }

        $params = $this->input->post();
        $data = $this->_checkParams($params);
        if (!$data) {
            $this->outputError($this->getError());
        }

        $this->load->model('wx/user/Social_Model', 'social');
        $user = $this->social->getUserByOpenId($this->openId);
        if (empty($user)) {
            $this->outputError('用户不存在');
        }

        $data['userId'] = $user['userid'];

        $this->load->model('wx/user/Address_Model', 'addr');
        $lists = $this->addr->add($data);

        if (!$lists) {
            $this->outputError('保存失败');
        } else {
            $this->output($lists);
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (empty($this->openId)) {
            $this->outputError('用户未登录');
        }

        $params = $this->input->post();
        $data = $this->_checkParams($params);
        if (!$data) {
            $this->outputError($this->getError());
        }

        if (empty($params['addressId'])) {
            $this->outputError('参数错误');
        }

        $data['addressId'] = $params['addressId'];
        $this->load->model('wx/user/Address_Model', 'addr');
        $lists = $this->addr->edit($data);

        if (!$lists) {
            $this->outputError('保存失败');
        } else {
            $this->output($lists);
        }
    }

    /**
     * 获取一条详情
     */
    public function getItem()
    {
        if (!empty($this->openId)) {

            $addressId = $this->input->get('addressId');
            if (empty($addressId)) {
                $this->outputError('地址信息不存在');
            }

            $this->load->model('wx/user/Address_Model', 'addr');
            $data = $this->addr->getItem($addressId);
            if ($data) {
                $this->output($data);
            } else {
                $this->outputError('删除失败');
            }


        } else {
            $this->outputError('用户未登录');
        }
    }

    /**
     * 删除
     */
    public function delete()
    {
        if (!empty($this->openId)) {

            $addressId = $this->input->get('addressId');
            if (empty($addressId)) {
                $this->outputError('参数错误');
            }

            $this->load->model('wx/user/Social_Model', 'social');
            $user = $this->social->getUserByOpenId($this->openId);
            if (empty($user)) {
                $this->outputError('用户不存在');
            }

            $this->load->model('wx/user/Address_Model', 'addr');
            $status = $this->addr->deleteItem($user['userid'], $addressId);
            if ($status) {
                $this->output('删除成功');
            } else {
                $this->outputError('删除失败');
            }

        } else {
            $this->outputError('用户未登录');
        }
    }

    /**
     * 设置默认
     */
    public function setDefault()
    {
        if (!empty($this->openId)) {

            $addressId = $this->input->post('addressId');
            if (empty($addressId)) {
                $this->outputError('参数错误');
            }

            $this->load->model('wx/user/Social_Model', 'social');
            $user = $this->social->getUserByOpenId($this->openId);
            if (empty($user)) {
                $this->outputError('用户不存在');
            }

            $this->load->model('wx/user/Address_Model', 'addr');
            $status = $this->addr->setDefault($user['userid'], $addressId);
            if ($status) {
                $this->output('设置成功');
            } else {
                $this->outputError('设置失败');
            }
        } else {
            $this->outputError('no login');
        }
    }

    /**
     * 检查参数
     */
    private function _checkParams(Array $params)
    {
        $needs = array('name', 'phone', 'province', 'city', 'area', 'address', 'default');
        $data = array();

        if ($this->input->method(true) !== 'POST') {
            $this->setError('非法请求');
            return false;
        }

        foreach($needs as $need) {
            if (!array_key_exists($need, $params)) {
                $this->setError('参数错误');
                return false;
            }
            $data[$need] = $params[$need];
        }

        if (!checkPhone($params['phone'])) {
            $this->setError('手机号格式错误');
            return false;
        }

        return $data;
    }
}
